#include "PlatformHolder.h"

std::list<Platform *> PlatformHolder::platforms;

void PlatformHolder::addPlatform(Platform *p) { platforms.push_back(p); }

void PlatformHolder::removeAllPlatforms(void) { platforms.clear(); }

Platform *PlatformHolder::CheckPlatformCollision(Sprite *s) {
	int x1, y1, x2, y2, x3, y3, x4, y4;
	Rect b1, b2;
	b1 = s->GetRect();
	x1 = s->GetX();
	y1 = s->GetY();
	x2 = x1+b1.w;
	y2 = y1+b1.h;
	std::list<Platform *>::iterator i = platforms.begin();
	while(i != platforms.end()) {
		b2 = (*i)->GetRect();
		//check for collision
		x3 = b2.x;
		y3 = b2.y;
		x4 = x3+b2.w;
		y4 = y3+b2.h;
		//printf("[(%d,%d),(%d,%d)], [(%d,%d),(%d,%d)]\n", x1, y1, x2, y2, x3, y3, x4, y4);
		if((!(x2 < x3) && !(x1 > x4)) && (!(y2 < y3) && !(y1 > y4))) {
			return *i;
		}
		i++;
	}
	return NULL;
}